<template>
  <div class="reply-comment">
    <van-popup :value="isShow" position="right" @open="open">
      <van-nav-bar
        :title="total + '条回复'"
        left-arrow
        @click-left="clickLeft"
      ></van-nav-bar>
      <div class="reply-wrapper list">
        <!-- 列表 -->
        <van-list
          v-if="bol"
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getReply"
        >
          <div
            class="item van-hairline--bottom"
            v-for="item in replyList"
            :key="item.com_id"
          >
            <van-image round width="10vw" height="10vw" :src="item.aut_photo" />
            <div class="info">
              <p>
                <span class="name">{{ item.aut_name }}</span>
                <span class="zan">
                  {{ item.like_count }}
                  <geek-icon name="like2"
                /></span>
              </p>
              <p class="cont">{{ item.content }}</p>
              <p>
                <span class="time" style="margin-left:0">
                  {{ item.pubfate | formatTime }}
                </span>
              </p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->
      <div class="footer van-hairline--top" style="position:static">
        <div class="input big" @click="showInput">
          <i class="van-icon van-icon-edit"></i>
        </div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon>
          <p>收藏</p>
        </div>
        <div class="btn">
          <geek-icon name="share"></geek-icon>
          <p>分享</p>
        </div>
      </div>
    </van-popup>
    <van-popup :value="inputShow" position="bottom">
      <van-nav-bar
        left-arrow
        :title="comment === '' ? '发表评论' : '回复评论'"
        right-text="发表"
        @click-left="inputShow = false"
        @click-right="sendComment"
      ></van-nav-bar>
      <van-field
        v-model="text"
        rows="3"
        autosize
        type="textarea"
        maxlength="100"
        placeholder="请输入评论"
        show-word-limit
      />
    </van-popup>
  </div>
</template>

<script>
import { commentsGet, commentsPost } from '@/api/article.js'
export default {
  name: 'ReplyComment',
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    comment: {
      type: [String, Object],
      default: ''
    },
    commentList: {
      type: Array,
      default: () => []
    },
    article: {
      type: Object,
      default: () => ({})
    }
  },
  model: {
    prop: 'isShow',
    event: 'closeShow'
  },
  data () {
    return {
      loading: false,
      finished: false,
      replyList: [],
      source: '',
      bol: true,
      inputShow: false,
      text: '',
      total: this.comment.com_count
    }
  },
  methods: {
    // 返回箭头点击事件
    clickLeft () {
      this.$emit('closeShow', false)
    },
    // 获取评论回复
    async getReply () {
      this.source = this.comment.com_id
      const res = await commentsGet({
        type: 'c',
        source: this.source
      })
      this.loading = false
      if (res.data.end_id === res.data.last_id) {
        this.finished = true
      } else {
        this.source = res.data.last_id
      }
      this.replyList.push(...res.data.results)
      this.total = res.data.total_count
    },
    // 打开评论回复弹出层前初始化
    open () {
      this.loading = false
      this.finished = false
      this.replyList = []
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 打开写评论弹出层
    showInput () {
      this.inputShow = true
    },
    // 发表评论
    async sendComment () {
      if (this.text === '') {
        return this.$toast.fail('请输入评论')
      }
      if (this.comment !== '') {
        const res = await commentsPost({
          target: this.comment.com_id,
          content: this.text,
          art_id: this.$route.params.articleId
        })
        this.replyList.unshift(res.data.new_obj)
        this.total++
        this.comment.reply_count++
      } else {
        const res = await commentsPost({
          target: this.$route.params.articleId,
          content: this.text
        })
        this.commentList.unshift(res.data.new_obj)
        this.article.comm_count++
      }
      this.inputShow = false
      this.text = ''
    }
  }
}
</script>

<style lang="less" scoped>
.reply-comment {
  .van-popup {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .reply-wrapper {
    width: 100%;
    height: 100%;
    overflow-y: auto;
  }

  .list {
    padding: 0 16px;

    .item {
      display: flex;
      padding: 10px 0;

      .info {
        padding-left: 10px;
        flex: 1;

        p {
          margin: 0;

          .name {
            font-size: 16px;
          }

          .zan {
            font-size: 14px;
            float: right;
            color: #999;

            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }

          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }

          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;

            .van-icon {
              position: relative;
              top: 1px;
            }
          }

          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }

  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 50px;
    background: #fff;
    display: flex;
    width: 100%;
    align-items: center;

    .input {
      margin-left: 10px;
      width: 200px;
      height: 34px;
      background: @geek-gray-color;
      border-radius: 17px;
      line-height: 36px;
      padding-left: 10px;

      .van-icon {
        color: #999;
      }

      &.big {
        width: 260px;
      }
    }

    .btn {
      flex: 1;
      text-align: center;
      position: relative;

      p {
        margin: 0;
        font-size: 10px;
      }

      .geek-icon {
        font-size: 18px;
      }

      i {
        height: 16px;
        min-width: 16px;
        padding: 0 3px;
        background: @geek-color;
        color: #fff;
        font-size: 10px;
        position: absolute;
        right: 0;
        top: -4px;
        line-height: 16px;
        border-radius: 8px;
        font-style: normal;
      }
    }
  }

  ::v-deep .van-nav-bar__text {
    color: @geek-color;
  }
  ::v-deep .van-field__control {
    background: @geek-gray-color;
    padding: 5px 10px;
    margin-bottom: 5px;
    border-radius: 4px;
  }
}
</style>
